Function Names and Terminology

The previous lesson, we showed you how to define your own functions. However, choosing a name for your function and the m-file that stores the function is also a very important part of defining new functions. This lesson helps you understand the terms that we use to describe and discuss functions and how to choose an appropriate name for your Matlab functions.

Save the m-file that contains the definition of your function with the same name that you choose for the function itself. Be sure to use the same capitalization as used for the function's name in the function header. This is the default in Matlab.

Function Naming Rules (must be followed)

Function names ...

  1. must begin with a letter.
  2. may contain any combination of letters, digits, and underscores.
  3. may not contain blank spaces or other types of punctuation.
  4. are case-sensitive. That means that myFunction is a different name than MyFunction.
  5. must be unique in the first 63 characters.

    Matlab uses only the first namelengthmax=63 characters of the name to distinguish one function from another. So be sure to keep this in mind when choosing function names.

Finally, be sure to save your function definition in a text-only m-file with the same name and same capitalization as the function. The function FtoC should be saved in a file named FtoC.m.

Function Naming Conventions (should be followed)

  1. Choose descriptive names. For example: squareMinusOne is a more descriptive name than sqMin1.
  2. Choose names with less than 20 characters or so. Shorter names are easier to remember (and type) than longer names.
  3. Avoid non-obvious abbreviations in your function names. Names that are easy to remember and use is the goal.
    • F is a common easy to remember abbreviation for Fahrenheit.
    • C is a common easy to remember abbreviation for Celsius.
    • m is a common easy to remember abbreviation for miles, meters, minutes, etc.
      Do you see the problem with using "m" as an abbreviation?
  4. If you use abbreviations in your function names, be sure to use the same abbreviation for that word in other function names.
  5. Use consistent capitalization. Always start with a lower case letter or always start with an upper case letter. Most users choose lower case letters to start their function names since they won't need to use the shift character to type the first letter. However, it is common to capitalize the first letter of additional words in the name. This is known as CamelCase and some programmers may even distiguish between UpperCamelCase and lowerCamelCase.
  6. Don't choose names that are already being used as variable or function names. Your code may still work, but you may be redefining a name that you may need to use later and you will likely not remember that you have redefined that name.

    Use the which -all command to see if the name you wish to use has already been used. It will return all current uses of that name. If none are found, you can use the name safely. For example, the following command will return all instances of the function named myFunction:

    which -all myFunction
Function Terminology

Matlab, like all programming languages, requires a very specific form for defining functions. Learn the following terminology to help you understand the anatomy and the required form of all functions, including those defined by you.

Function Name A name that you pick that will be used to call or execute the function. It must follow the naming rules and it should follow the naming conventions.
Parameter(s) A list of variable names that store the arguments that the function needs to calculate its return value.
Return Value A value that is returned by a function. You must pick a variable name for this value and name it in the function header.
Header The first line of a function definition file. It includes the reserved word function, a variable for storing the return value, an equal sign, the name of the function, and the input argument list. The variable that is returned by this function is tempC, the name of this function is FtoC, and the input parameter is tempF.
    function tempC = FtoC ( tempF )
Comments Include comments after the header to describe what the function does, the inputs to the function, and the return value.
    % This function returns the temperature in celsius
    % tempF contains the temperature, in fahrenheit, to be converted
Body The actual code lines that calculate a value or vector of values. Includes an assignment statement to assign the calculated value to the return value variable, named in the function header. Comments on lines in the body are only required if the value being calculated is too complex to understand directly.
    tempC = (tempF - 32)*5/9;

Note: The temperature calculation above is very short, but you will have some calculations that take many more characters to enter. If your calculation is too long (>80 chars) to fit on one line, you should use an ellipsis ... to indicate that the calculation continues and enter the remaining calculations on the lines that follow. This makes the function definition more readable since the programmer (you) won't have to scroll to the right and back to see the entire calculation. For example, this calculation is split on four lines to show each term:

y = 1/(x+0.042345*x^2) ...
    + 2/(x+0.523464*x^3) ...
    + 3/(x+0.238952*x^4) ...
    + 4/(x+0.234903*x^5)...
    + 5/(x+0.230945*x^6) ...
    + 6/(x+0.045923*x^7) ...
    + 7/(x+0.203950*x^8);

The continuation symbol (...) can be put between before or after operators. Choose a place that makes your code easy to read.